<template>
  <div style="width: 100%" class="offline-item">
    <el-button type="primary" style="margin-bottom: 16px" size="small" @click="addItem">新增</el-button>
    <el-table :data="tableData" border>
      <el-table-column prop="sort" label="序号" align="center" width="120">
        <template slot-scope="scope">
          <span>{{ parseInt(scope.row.sort) + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="time" width="360px" label="任务完成时长">
        <template slot-scope="scope">
          <el-input-number :controls="true" v-model="scope.row.time" :min="1" label="任务完成时长（秒)"
            @change="tableChange"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column prop="url" label="操作" width="300">
        <template slot-scope="scope">
          <el-button type="danger" size="mini" style="margin-left: 16px" @click="remove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { randomvideo } from "@/api/financial/offlineLlist";
export default {
  props: {
    list: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      tableData: [],
    };
  },
  created() {
    this.tableData = this.list;
  },
  watch: {
    list: {
      immediate: true,
      deep: true,
      handler(newVal, oldVal) {
        this.$nextTick(() => {
          this.tableData = newVal;

        })
      }
    }
  },
  methods: {
    getVideo(item) {
      randomvideo().then((res) => {
        const url = res.data.url;
        //去重
        for (let it of this.tableData) {
          if (it.url === url) {
            this.getVideo(it);
            return;
          }
        }
        item.url = url;
        this.tableChange();
      });
    },
    addItem() {
      const oldCount = this.tableData.length;
      this.tableData.push({
        sort: oldCount,
        time: undefined,
        gold: undefined,
        url: undefined,
      });
      this.tableChange();
    },
    remove({ sort }) {
      const oldList = this.tableData;
      oldList.splice(sort, 1);
      const newList = [];
      for (let index in oldList) {
        let item = oldList[index];
        item.sort = index;
        newList.push(item);
      }
      this.tableData = newList;
      this.tableChange();
    },
    tableChange() {
      this.$emit("tableChange", this.tableData);
    },
  },
};
</script>
<style>
.offline-item .el-table__empty-block {
  height: 50px !important;
}
</style>